package com.gcd.gym.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcd.gym.model.Reserve;
import com.gcd.gym.model.searchModel.ReserveSearchModel;
import com.gcd.gym.service.ReserveService;
import com.gcd.gym.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api/v1/reserve",produces = MediaType.APPLICATION_JSON_VALUE)
public class ReserveApi {
    private ReserveService reserveService;

    @Autowired
    public void setReserveService(ReserveService reserveService) {
        this.reserveService = reserveService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findReserve(@RequestParam(defaultValue = "1") Integer pageNo,
                                                     @RequestParam(defaultValue = "5") Integer pageSize,
                                                     ReserveSearchModel reserve) {

        Page<ReserveSearchModel> page=new Page<>(pageNo,pageSize);
        page=reserveService.findReserve(page,reserve);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    @PostMapping(value = "/{id}/cancel")
    public ResponseEntity<JsonResult<?>> cancelReserve(@PathVariable Integer id) {
        boolean result=reserveService.cancelReserve(id);
        if(result) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("取消预约失败！"));
        }

    }


    @PutMapping
    public ResponseEntity<JsonResult<?>> editReserve(@RequestBody Reserve reserve) {
        boolean result=reserveService.editReserve(reserve);
        if(result) {
            return ResponseEntity.ok(JsonResult.success());
        } else {
            return ResponseEntity.ok(JsonResult.fail("修改预约失败！"));
        }
    }
}
